//
//  HomeViewController.swift
//  SwiftDemo
//
//  Created by 李国卿 on 2020/1/16.
//  Copyright © 2020 guoqing.li. All rights reserved.
//

import UIKit

private let kTitleViewH: CGFloat = 40

class HomeViewController: UIViewController {
    
    // 懒加载
   private lazy var pageTitleView: PageTitleView = {[weak self] in
        let frame = CGRect(x: 0, y: kNavigationBarH, width: kScreenW, height: kTitleViewH)
        let titles = ["推荐","娱乐","游戏","趣玩"]
        let pageTitleView = PageTitleView(frame: frame, titles: titles)
        pageTitleView.delegate = self;
        return pageTitleView;
    }()
    lazy var pageContentView: PageContentView = {[weak self] in
        let pageContentH = kScreenH - kNavigationBarH - kTitleViewH - kNavigationBarH;
        let pageContentF = CGRect(x: 0, y: kNavigationBarH + kTitleViewH, width: kScreenW, height: pageContentH)
        
        var childVcs = [UIViewController]()
        childVcs.append(RecommendViewController())
        childVcs.append(GameViewController())
        childVcs.append(AmuseViewController())
        childVcs.append(FunnyViewController())
        let pageContentView = PageContentView.init(frame: pageContentF, childVcs: childVcs, parentVC: self)
        pageContentView.delegate = self
        return pageContentView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    

}

extension HomeViewController
{
    private func setupUI() {
        setNavigationItemBar()
        setPageTitleView()
        setPageContentView()
    }
}

extension HomeViewController {
    private func setPageTitleView(){
        view.addSubview(pageTitleView);
    }
}

extension HomeViewController {
    private func setPageContentView(){
        view.addSubview(pageContentView);
    }
}

extension HomeViewController : PageTitleViewDelegate {
    func pageTitleView(titleView: PageTitleView, selectIndex: Int) {
        pageContentView.setContentIndex(currentIndex: selectIndex)
    }
}

extension HomeViewController : PageContentViewDelegate {
    func pageContentView(contentView: PageContentView, progress: CGFloat, sourceIndex: Int, targetIndex: Int) {
        pageTitleView.setTitleWithProgress(progress: progress, sourceIndex: sourceIndex, targetIndex: targetIndex)
    }
}

extension HomeViewController
{
    private func setNavigationItemBar(){
        // 1. 设置左侧item
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "logo", viewController: self, selector: #selector(logooAction))
        // 2. 设置右侧item
        let size = CGSize(width: 40, height: 40)
        let historyItem = UIBarButtonItem(imageName: "image_my_history", highImageName: "Image_my_history_click", size: size, viewController: self, selector: #selector(historyAction))
        let searchItem = UIBarButtonItem(imageName: "btn_search", highImageName: "btn_search_clicked", size: size, viewController: self, selector: #selector(searchAction))
        let qrscanItem = UIBarButtonItem(imageName: "Image_scan", highImageName: "Image_scan_click", size: size, viewController: self, selector: #selector(scanAction))
        self.navigationItem.rightBarButtonItems = [historyItem,searchItem,qrscanItem];
    }
}

// MARK - 点击方法
extension HomeViewController
{
    @objc fileprivate func logooAction (){
        print("logoAction")
    }
    @objc fileprivate func historyAction (){
        print("historyAction")
    }
    @objc fileprivate func searchAction (){
        print("searchAction")
    }
    @objc fileprivate func scanAction (){
        print("scanAction")
    }

}
